﻿using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CutShuFa.AutoAction
{
    public class ActionFillContours : AAutoAction
    {
        public override string ActionName
        {
            get
            {
                return "填充轮廓";
            }
        }

        public override List<Parameter> GetParameters()
        {
            if (parameters == null)
            {
                parameters = new List<Parameter>();
                parameters.Add(new Parameter() { Name = "output", Value = "tmp_mat", Type = ParameterType.Mat });
                parameters.Add(new Parameter() { Name = "color", Value = "#FF00FF", Type = ParameterType.Color });
            }
            return parameters;
        }

        public override void Run(AutoActionData data)
        {
            if (data.contours == null)
            {
                return;
            }
            if (this.parameters == null)
            {
                GetParameters();
            }
            string output = parameters.Where(p => p.Name == "output").FirstOrDefault().Value;

            // -1 表示绘制所有轮廓，否则绘制指定索引的轮廓
            Scalar color = ToScalar(parameters.Where(p => p.Name == "color").FirstOrDefault().Value);
            // 如果是灰度图，则将color转为灰度颜色
            if (data.matDic[output].Channels() == 1)
            {
                color = CvHelper.ToGrayScalar(color);
            }
            Cv2.FillPoly(data.matDic[output], data.contours, color);
        }

    }
}
